<?php
class CruiseController extends Apps_Controller_Action_Default {

	public function indexAction() {
		$this->_redirect('/cruise/category');
	}

	// Show Cruise Category
	public function categoryAction() { 
		$rq         = $this->_request;
		$categoryId = intval($rq->getParam('id'));

		$validator  = new Zend_Validate_Db_RecordExists(array('table' => 'cruise_category', 'field' => 'id'));
		if($categoryId && $validator->isValid($categoryId)) {
    		$cruiseItineraryTourModel = new M_CruiseItineraryTour();
            $cruiseCategoryModel 	  = new M_CruiseCategory();

            $cruiseCategory = $cruiseCategoryModel->getCruiseCategoryInfo($categoryId)->query()->fetch();
            $this->view->cruiseCategory = $cruiseCategory;

            //LIST CRUISE BY TYPE 
            $type = 'best_selling';
			$listCruise  = $cruiseItineraryTourModel->listCruiseTourByType($categoryId, $type);
			if($listCruise) {
				$this->setPagination($listCruise, $rq);
			}
			
			switch ($type) {
				case 'best_selling':
					$this->view->titleContent = 'Best_Selling_Cruise_Package';
				break;
				case 'special_offer':
					$this->view->titleContent = 'Promotion_Cruise_Package';
				break;
				case 'featured':
					$this->view->titleContent = 'Featured_Cruise_Package';
				break;
			}
			
			//COLUMNS LEFT
			$this->view->columnLeft = $this->getColumnLeft($categoryId);
			
		} else {
			$this->goToControllerIndex();
		}

			
		//meta tag
		$tbVctSetting = new M_DbTable_VctSetting();
		$vctSetting   = $tbVctSetting->getMetaTag();
		$this->_page_meta_keywords    = $cruiseCategory['meta_keyword'] ? $cruiseCategory['meta_keyword'] : ($vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword);
		$this->_page_meta_description = $cruiseCategory['meta_description'] ? $cruiseCategory['meta_description'] : ($vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description);
		$this->_page_title 	  		  = $cruiseCategory['header_title'] ? $cruiseCategory['header_title'] : ($vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title);
	}


	//to render view
	public function getColumnLeft($categoryId) {
		if(!$categoryId) {
			return null;
		}
	    $cruiseModel     	 = new M_Cruise();
	    $cruiseTypeModel 	 = new M_CruiseType();
	    $cruiseItineraryTourModel = new M_CruiseItineraryTour();

        $cruiseType = $cruiseTypeModel->getList()->query()->fetchAll();
        if($cruiseType) {
            foreach($cruiseType as $k => $v) {
            	$listParentCruise = $cruiseModel->listCruiseMenuLeft($categoryId, $v['id'])->query()->fetchAll();
            	foreach($listParentCruise as $key => $value) {
            		$listCruise = $cruiseModel->listCruiseMenuLeft($categoryId, $v['id'], $value['id'])->query()->fetchAll();
            		
            		if($listCruise) {
	            		foreach($listCruise as $cK => $cruise) {
	           				$cruiseItineraryTour = $cruiseItineraryTourModel->getCruiseItineraryTourByCruiseId($cruise['id'])->query()->fetch();
	            			if($cruiseItineraryTour) {
								$listCruise[$cK]['link_id']    = $cruiseItineraryTour['id'];
								$listCruise[$cK]['link_title'] = $cruiseItineraryTour['title'];
	            			} else {
								$listCruise[$cK]['link_id']    = -1;
								$listCruise[$cK]['link_title'] = $cruise['name'];
	            			}
	            		}
            			$listParentCruise[$key]['link_id']    = -1;
            			$listParentCruise[$key]['link_title'] = $cruise['name'];
						$listParentCruise[$key]['list_child_cruise'] = $listCruise;            		
            		} else {
            			$cruiseItineraryTour = $cruiseItineraryTourModel->getCruiseItineraryTourByCruiseId($value['id'])->query()->fetch();
            			if($cruiseItineraryTour) {
	            			$listParentCruise[$key]['link_id']    = $cruiseItineraryTour['id'];
	            			$listParentCruise[$key]['link_title'] = $cruiseItineraryTour['title'];
            			} else {
	            			$listParentCruise[$key]['link_id']    = -1;
	            			$listParentCruise[$key]['link_title'] = $value['name'];
            			}
            		}
            	}
                $cruiseType[$k]['list_cruise'] = $listParentCruise; 
            }
            return $cruiseType;
        }
        return null;
	}

	public function detailAction() {
		$rq = $this->_request;
		$id = intval($rq->getParam('id')); //Cruise Itienrary Tour Id
		if(!$id) {
			$this->_redirect('');
		}
		
		$validator = new Zend_Validate_Db_RecordExists(array('table' => 'cruise_itinerary_tour', 'field' => 'id'));
		if($validator->isValid($id)) {
    		$cruiseItineraryTourModel = new M_CruiseItineraryTour();
			$cruiseItineraryTour	  = $cruiseItineraryTourModel->getCruiseItineraryTourInfo($id)->query()->fetch();
			
			//CRUISE TOUR DETAIL
			$this->view->cruise = $cruiseItineraryTour;
			
			//OTHER CRUISE TOUR OF CRUISE
			$cruise = $cruiseItineraryTourModel->getCruiseByCruiseItineraryTourId($id)->query()->fetch();
			$this->view->otherLink = $cruiseItineraryTourModel->listOtherCruiseTourByCruiseId($cruise['cruise_id'], $id)->query()->fetchAll();
			$this->view->cruiseId  = $cruise['cruise_id'];

            //PRICE CRUISE
			$tbCruiseCabinType      = new M_DbTable_CruiseCabinType();
			$tbCruiseCabinTypePrice = new M_DbTable_CruiseCabinTypePrice();
			$cruiseCabinType 		= $tbCruiseCabinType->fetchAll(array('cruise_itinerary_tour_id =?' => $id))->toArray();
			foreach($cruiseCabinType as $k => $v) {
			    $cruiseCabinType[$k]['price'] = $tbCruiseCabinTypePrice->fetchAll(array('cruise_itinerary_cabin_type_id =?' => $v['id']))->toArray();
			}
			$this->view->cruiseCabin = $cruiseCabinType;

			//LEFT COLUMN
			$this->view->columnLeft = $this->getColumnLeft($cruiseItineraryTour['category_id']);
			
			//SEO
			$tbVctSetting = new M_DbTable_VctSetting();
			$vctSetting   = $tbVctSetting->getMetaTag();
			$this->_page_title 	  		  = $cruiseItineraryTour['header_title'] ? $cruiseItineraryTour['header_title'] : ($vctSetting['header_title'] ? $vctSetting['header_title'] : $this->_default_header_title);
			$this->_page_meta_keywords    = $cruiseItineraryTour['meta_keyword'] ? $cruiseItineraryTour['meta_keyword'] : ($vctSetting['meta_keyword'] ? $vctSetting['meta_keyword'] : $this->_default_keyword);
			$this->_page_meta_description = $cruiseItineraryTour['meta_description'] ? $cruiseItineraryTour['meta_description'] : ($vctSetting['meta_description'] ? $vctSetting['meta_description'] : $this->_default_description);
		} 
		else {
			$this->view->cruise = null;
			
			//LEFT COLUMN
			$tbCruiseCategory = new M_DbTable_CruiseCategory();
			$cruiseCategory   = $tbCruiseCategory->fetchRow();
			if($cruiseCategory) {
				$this->view->columnLeft = $this->getColumnLeft($cruiseCategory['id']);
			}
		}
		
// 			Zend_Debug::dump($this->view->columnLeft);die();
			
		$this->view->categoryTitle = $rq->getParam('category_title');
	}
}